<template>
  <div class="set_user">
    <div class="user">
      <div class="user_top">
        <div class="left">
          <el-avatar
            :src="userinfo.avatar"
            shape="square"
            :size="50"
          ></el-avatar>
        </div>
        <div class="right">
          <div class="name">用户名:{{ userinfo.username }}</div>
          <div class="sign">签名: {{ sign }}</div>
        </div>
      </div>
      <div class="content">
        <label for="area" class="item"
          ><span class="item_text">地区:</span>
          <el-input
            v-model="userinfo.area"
            placeholder="请输入地区..."
          ></el-input>
        </label>
        <label for="email" class="item"
          ><span class="item_text">邮箱:</span>
          <el-input
            v-model="userinfo.email"
            placeholder="请输入邮箱..."
          ></el-input>
        </label>
        <label for="sign" class="item"
          ><span class="item_text">签名:</span>
          <el-input
            v-model="userinfo.sign"
            type="textarea"
            class="textarea"
            placeholder="请输入签名..."
          ></el-input>
        </label>
        <div class="btn">
          <el-button type="primary" :disabled="isDisabled" @click="editUSerInfo"
            >保存设置</el-button
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, watch } from 'vue'

import useUser from '@renderer/pinai/modules/user'
import { message } from '@renderer/utlis/feedback'

const usetStore = useUser()

const userinfo = reactive({
  // 展示的用户信息
  username: usetStore.userInfo.username as string,
  area: usetStore.userInfo.area as string,
  email: usetStore.userInfo.email as string,
  sign: usetStore.userInfo.sign as string,
  avatar: usetStore.userInfo.avatar as string
})
const sign = ref(usetStore.userInfo.sign) // 签名

const isDisabled = ref(true) // 是否禁止保存按钮

// 监听用户信息变化
watch(userinfo, () => {
  isDisabled.value = false
})

// 修改用户信息
const editUSerInfo = () => {
  usetStore.updateUserInfo(userinfo).then(() => {
    sign.value = userinfo.sign
    message('修改成功')
  })
}
</script>

<style lang="scss" scoped>
.set_user {
  display: flex;
  justify-content: center;
  .user {
    width: 80%;
    margin-top: 50px;
    padding: 10px;
    border-radius: 10px;
    box-sizing: border-box;
    background-color: #f5f5f5;

    .user_top {
      display: flex;
      align-items: center;
      .left {
        margin-right: 15px;
      }
      .right {
        flex: 1;
        font-size: 14px;

        .sign {
          width: 250px;
          white-space: nowrap;
          text-overflow: ellipsis;
          overflow: hidden;
        }
      }
    }

    .content {
      margin-top: 20px;

      .item {
        display: flex;
        align-items: center;
        margin-bottom: 10px;
        .item_text {
          white-space: nowrap;
          margin-right: 8px;
        }

        .textarea {
          resize: none;
        }
      }

      .btn {
        display: flex;
        justify-content: center;
      }
    }
  }
}
</style>
